package com.iclippi.test.server;

import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.appengine.api.datastore.dev.LocalDatastoreService;
import com.google.appengine.tools.development.ApiProxyLocalImpl;
import com.google.apphosting.api.ApiProxy;
import com.iclippi.server.model.PMF;

public class LocalDatastoreTestCase extends LocalServiceTestCase {

	@Override
	public void setUp() throws Exception {
		super.setUp();

		//Remove the annoying datastore debug logs
		Logger.getLogger("DataNucleus.Transaction").setLevel(Level.OFF);
		Logger.getLogger("DataNucleus.MetaData").setLevel(Level.OFF);
        Logger.getLogger("DataNucleus.Plugin").setLevel(Level.OFF);
        Logger.getLogger("DataNucleus.Persistence").setLevel(Level.OFF);
        
		ApiProxyLocalImpl proxy = (ApiProxyLocalImpl) ApiProxy.getDelegate();
		proxy.setProperty(LocalDatastoreService.NO_STORAGE_PROPERTY,
				Boolean.TRUE.toString());
		
		Properties newProperties = new Properties();
		newProperties.put("javax.jdo.PersistenceManagerFactoryClass",
				"org.datanucleus.store.appengine.jdo."
						+ "DatastoreJDOPersistenceManagerFactory");
		newProperties.put("javax.jdo.option.ConnectionURL", "appengine");
		newProperties.put("javax.jdo.option.NontransactionalRead", "true");
		newProperties.put("javax.jdo.option.NontransactionalWrite", "true");
		newProperties.put("javax.jdo.option.RetainValues", "true");
		newProperties.put("datanucleus.appengine.autoCreateDatastoreTxns",
				"true");
		
		PMF.setProperties(newProperties);
	}

	@Override
	public void tearDown() throws Exception {
		ApiProxyLocalImpl proxy = (ApiProxyLocalImpl) ApiProxy.getDelegate();
		LocalDatastoreService datastoreService = (LocalDatastoreService) proxy
				.getService("datastore_v3");
		datastoreService.clearProfiles();
		super.tearDown();
	}

}
